In [32]:
# Objects and Data Structures Assessment Test

Test your knowledge.

Answer the following questions

Write a brief description of all the following Object Types and Data Structures we've learned about:

Numbers:

Strings:

Lists:

Tuples:

Dictionaries:

Numbers

Write an equation that uses multiplication, division, an exponent, addition, and subtraction that is equal to 100.25.

Hint: This is just to test your memory of the basic arithmetic commands, work backwards from 100.25


In [6]:
10*10/1 + 2**2 - 3.75


Out[6]:
100.25

Explain what the cell below will produce and why. Can you change it so the answer is correct? It will produce 0.6666 because it is python 3 so will use real division


In [7]:
2/3


Out[7]:
0.6666666666666666

Answer these 3 questions without typing code. Then type code to check your answer.

What is the value of the expression 4 * (6 + 5) 
    44

What is the value of the expression 4 * 6 + 5 
    29

What is the value of the expression 4 + 6 * 5 
    34

In [10]:
4 + 6 * 5


Out[10]:
34

What is the type of the result of the expression 3 + 1.5 + 4? It is a float because one of the number is a floating point

LaTex: a not sure how to get the

What would you use to find a number’s square root, as well as its square?


In [17]:
print(2**2)    # squared (raise to the power of 2)
print(2**0.5)  # square root (raise to the power of 1/2)


4
1.4142135623730951

Strings

Given the string 'hello' give an index command that returns 'e'. Use the code below:


In [25]:
s = 'hello'
# Print out 'e' using indexing
# Code here
s[1]


Out[25]:
'e'

Reverse the string 'hello' using indexing:


In [23]:
s ='hello'

# Reverse the string using indexing
print (s[::-1])
# Code here


olleh
o

Given the string hello, give two methods of producing the letter 'o' using indexing.


In [35]:
s ='hello'

# Print out the

# Code here
print(s[-1])
print(s[4])


o
o

Lists

Build this list [0,0,0] two separate ways.


In [75]:
l = [0,0,0]
print (l)
l = [0]*3
print(l)
l1 = list()
l1.append(0)
l1.append(0)
l1.append(0)
print(l1)


[0, 0, 0]
[0, 0, 0]
[0, 0, 0]

Reassign 'hello' in this nested list to say 'goodbye' item in this list:


In [7]:
l = [1,2,[3,4,'hello']]
l1 = l[2]
l1[l1.index('hello')] = 'goodbye'
print(l)


[1, 2, [3, 4, 'goodbye']]

Sort the list below:


In [77]:
l = [5,3,4,6,1]
l.sort()
print(l)
l = [5,3,4,6,1]
print(sorted(l))


[1, 3, 4, 5, 6]
[1, 3, 4, 5, 6]

Dictionaries

Using keys and indexing, grab the 'hello' from the following dictionaries:


In [19]:
d = {'simple_key':'hello'}
# Grab 'hello'
print(d['simple_key'])


hello

In [22]:
d = {'k1':{'k2':'hello'}}
# Grab 'hello'
print(d['k1']['k2'])


hello

In [78]:
# Getting a little tricker
d = {'k1':[{'nest_key':['this is deep',['hello']]}]}

#Grab hello
print(d['k1'][0]['nest_key'][1][0])


hello

In [33]:
# This will be hard and annoying!
d = {'k1':[1,2,{'k2':['this is tricky',{'tough':[1,2,['hello']]}]}]}

In [92]:
d = {'k1':[1,2,{'k2':['this is tricky',{'tough':[1,2,['hello']]}]}]}# print it all
print(d)
# get the set at 'k1'
my_set = d['k1']
print('my_set: ' + str(my_set))
# get the dictionary at index 2 of the set
my_dict = my_set[2]
print('my_dict: ' + str(my_dict))
my_set1 = my_dict['k2']
print ('my_set1: ' + str(my_set1))
my_dict2 = my_set1[1]
print ('my_dict2: ' + str(my_dict2))
my_set2 = my_dict2['tough']
print ('my_set2: ' + str(my_set2))
my_set3 = my_set2[2]
print('my_set3: ' + str(my_set3))
indx = my_set3.index('hello')
print('indx = ' + str(indx))
print(my_set3[indx])

print (d['k1'][2]['k2'][1]['tough'][2][0])


{'k1': [1, 2, {'k2': ['this is tricky', {'tough': [1, 2, ['hello']]}]}]}
my_set: [1, 2, {'k2': ['this is tricky', {'tough': [1, 2, ['hello']]}]}]
my_dict: {'k2': ['this is tricky', {'tough': [1, 2, ['hello']]}]}
my_set1: ['this is tricky', {'tough': [1, 2, ['hello']]}]
my_dict2: {'tough': [1, 2, ['hello']]}
my_set2: [1, 2, ['hello']]
my_set3: ['hello']
indx = 0
hello
hello

Can you sort a dictionary? Why or why not? you cant sort a dictionary because it is keys and values

Tuples

What is the major difference between tuples and lists? tuples are immutable and lists are mutable

How do you create a tuple? with parentheses and a comma delimited list (1,2.3)

Sets

What is unique about a set? a set contains only unique items and is immutable

Use a set to find the unique values of the list below


In [68]:
l = [1,2,2,33,4,4,11,22,3,3,2]
print (l)
s = set(l)
print (s)


[1, 2, 2, 33, 4, 4, 11, 22, 3, 3, 2]
{1, 2, 33, 4, 3, 11, 22}

Booleans

For the following quiz questions, we will get a preview of comparison operators:

OperatorDescriptionExample
== If the values of two operands are equal, then the condition becomes true. (a == b) is not true.
!= If values of two operands are not equal, then condition becomes true.
<> If values of two operands are not equal, then condition becomes true. (a <> b) is true. This is similar to != operator.
> If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true.
< If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true.
>= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true.
<= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.

What will be the resulting Boolean of the following pieces of code (answer fist then check by typing it in!)


In [69]:
# Answer before running cell
2 > 3
#false


Out[69]:
False

In [70]:
# Answer before running cell
3 <= 2
#false


Out[70]:
False

In [71]:
# Answer before running cell
3 == 2.0
#false


Out[71]:
False

In [72]:
# Answer before running cell
3.0 == 3
#true


Out[72]:
True

In [73]:
# Answer before running cell
4**0.5 != 2
#false


Out[73]:
False

Final Question: What is the boolean output of the cell block below?


In [74]:
# two nested lists
l_one = [1,2,[3,4]]
l_two = [1,2,{'k1':4}]

#True or False?
#3>=4
#false
l_one[2][0] >= l_two[2]['k1']


Out[74]:
False

Great Job on your first assessment!